﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Actuals.Business;
using Actuals.CoreDomain;
using Actuals.CoreDomain.Business;

namespace Actuals
{
    public partial class AddActuals : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["EmployeeId"] == null)
            {
                Response.Write("Please login!");
                Response.End();
            }
            Response.AddHeader("Cache-control", "no-store, must-revalidate,private,no-cache");
            Response.AddHeader("Pragma", "no-cache");
            Response.AddHeader("Expires", "0");

            //if (Request.QueryString["ProjectId"] != null)
            //{
            //    int projectId = int.Parse(Request.QueryString["ProjectId"].ToString());
            //    int releaseId = int.Parse(Request.QueryString["ReleaseId"].ToString());
            //    ViewState["ProjectId"] = projectId;
            //    ViewState["ReleaseId"] = releaseId; 
            //}
            

            Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache);
            if (!Page.IsPostBack)
            {
                IBusinessProject businessproject = new BusinessProject();
                List<IProject> projects = businessproject.GetProjects();
                ddlProject.DataSource = projects;
                ddlProject.DataTextField = "ProjectDescription";
                ddlProject.DataValueField = "ProjectId";
                ddlProject.DataBind();
                ddlProject.Items.Insert(0, "Select");

              

                txtActualDate.Text = DateTime.Now.Date.ToString("MM-dd-yyyy");

                if (Request.QueryString["Mode"] != null)
                {

                    string _mode = Request.QueryString["Mode"].ToString();
                    ViewState["Mode"] = _mode;  
                    string _actualId = Request.QueryString["ActualId"].ToString();
                    hidActualId.Value = _actualId;
                    PopulateActual(Int64.Parse(_actualId));
                }
            }

          
        }

        protected void FillTask(string projectId, string releaseid)
        {
            IBusinessTask businessTask = new BusinessTask();
            IList<ITask> tasks = businessTask.GetTasks(int.Parse(projectId), int.Parse(releaseid));
            ddlTask.DataSource = tasks;
            ddlTask.DataValueField = "TaskId";
            ddlTask.DataTextField = "TaskShortName";
            ddlTask.DataBind();
            ddlTask.Items.Insert(0, "Select");
        }

        protected void ddlRelease_OnSelectedIndexChanged(Object sender, EventArgs e)
        {
            FillTask(ddlProject.SelectedValue.ToString(),
                ddlRelease.SelectedValue.ToString());
        }

        protected void ddlProject_OnSelectedIndexChanged(Object sender, EventArgs e)
        {
            FillRelease(ddlProject.SelectedValue);
        }

        protected void FillRelease(string projectId)
        {
            IBusinessRelease businessRelease = new BusinessRelease();
            IList<IRelease> releases = businessRelease.GetReleases(int.Parse(projectId));
            ddlRelease.DataSource = releases;
            ddlRelease.DataTextField = "Description";
            ddlRelease.DataValueField = "ReleaseId";
            ddlRelease.DataBind();
            ListItem select = new ListItem();
            select.Value = "0";
            select.Text = "Select";
            ddlRelease.Items.Insert(0, select);
        }

        protected void PopulateActual(Int64 actualId)
        {
            IBusinessActual businessActual = new BusinessActual();
            IActual actual = businessActual.GetActual(actualId);

            ddlTask.SelectedValue = actual.TaskId.ToString();
            FillSubTasks(actual.TaskId.ToString());
            
            ddlSubTask.SelectedValue = actual.SubTaskId.ToString();
            IBusinessEmployee businessEmployee = new BusinessEmployee();
            IEmployee employee = businessEmployee.GetEmployee(actual.EmployeeId);

            if (employee.RoleId == 2)
            {
                FillBASubTasks();
                ddlBASubTask.SelectedValue = actual.BASubTaskId.ToString();
            }
            else
            {
                ListItem select = new ListItem();
                select.Value = "0";
                select.Text = "Select";
                ddlBASubTask.Items.Insert(0, select);
            }
            txtDescription.Text = actual.Description;
            txtNoOfHours.Text = actual.NoOfHours.ToString();
            txtActualDate.Text = actual.date.Date.ToString("MM-dd-yyyy");

            ddlProject.SelectedValue = actual.ProjectId.ToString();
            FillRelease(actual.ProjectId.ToString());
            ddlRelease.SelectedValue = actual.ReleaseId.ToString();

            FillTask(actual.ProjectId.ToString(), actual.ReleaseId.ToString());



        }

        protected void btnSave_Click(object sender, EventArgs e)
        {


            try
            {
                IActual actual = new Actual();

                actual.ProjectId = int.Parse(ddlProject.SelectedValue);
                actual.ReleaseId = int.Parse(ddlRelease.SelectedValue);
                actual.TaskId = int.Parse(ddlTask.SelectedValue);
                actual.SubTaskId = int.Parse(ddlSubTask.SelectedValue);
                actual.NoOfHours = int.Parse(txtNoOfHours.Text);
                actual.Description = txtDescription.Text;
                actual.EmployeeId = int.Parse(Session["EmployeeId"].ToString()); // int.Parse(Session["EmpId"].ToString());
                actual.BASubTaskId = int.Parse(ddlBASubTask.SelectedValue);


                string[] _date = txtActualDate.Text.Split('-');

                actual.date = new DateTime(int.Parse(_date[2]), int.Parse(_date[0]), int.Parse(_date[1]));

                BusinessActual businessActual = new BusinessActual();

                if (ViewState["Mode"] != null)
                {
                    actual.ActualId = Int64.Parse(hidActualId.Value);
                    businessActual.UpdateActual(actual);

                }
                else
                    businessActual.AddActual(actual);
                lblMsg.Text = "Saved";
            }
            catch (Exception ex)
            {

                lblMsg.Text = ex.Message; 
            }
           
           
        }

        protected void ddlTask_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            if (Session["RoleId"].ToString() == "2")
                FillBASubTasks();
            else
            {
                ddlBASubTask.Items.Clear();
                ListItem select = new ListItem();
                select.Value = "0";
                select.Text = "Select";
                ddlBASubTask.Items.Insert(0, select);
            }
           FillSubTasks(ddlTask.SelectedValue);
        }

        protected void FillSubTasks (string taskId)
        {
            IBusinessSubTask businessSubTask = new BusinessSubTask();
            IList<ISubTask> subTasks = businessSubTask.GetSubTasks(int.Parse(taskId));
            ddlSubTask.DataSource = subTasks;
            ddlSubTask.DataValueField = "SubTaskId";
            ddlSubTask.DataTextField= "Description";
            ddlSubTask.DataBind();
            ListItem select = new ListItem();
            select.Value = "0";
            select.Text = "Select";

            ddlSubTask.Items.Insert(0, select);

        }

        protected void FillBASubTasks()
        {
            IBusinessBASubTask businessBaSubTask = new BusinessBASubTask();
            IList<IBASubTask> baSubTasks = businessBaSubTask.GetBASubTasks();
            ddlBASubTask.DataSource = baSubTasks;
            ddlBASubTask.DataValueField = "TaskId";
            ddlBASubTask.DataTextField = "TaskDescription";
            ddlBASubTask.DataBind();
            ListItem select = new ListItem();
            select.Value = "0";
            select.Text = "Select";

            ddlBASubTask.Items.Insert(0, select);

        }

      

    }
}
